package com.mtr.databoard.domain.resources;

import com.mtr.databoard.domain.Entity;
import org.bson.types.Binary;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Field;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class Resource  extends Entity<String> {
    private  String fileName;
    private  long size;
    private  String type;
    private List<ResourceEntity> resourceEntities;

    public Resource() {

    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public long getSize() {
        return size;
    }

    public void setSize(long size) {
        this.size = size;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public List<ResourceEntity> getResourceEntities() {
        return resourceEntities;
    }

    public void setResourceEntities(List<ResourceEntity> resourceEntities) {
        this.resourceEntities = resourceEntities;
    }

    public Resource(String fileId, String fileName, String type, long size) {
        this.setId(fileId);
        this.fileName = fileName;
        this.type = type;
        this.size =size;
    }

    public  void bindEntity(ResourceEntity entity)
    {
        if(this.resourceEntities==null)
            this.resourceEntities = new ArrayList<>() ;

        if(!this.resourceEntities.contains(entity))
            this.resourceEntities.add((entity));
    }
    public  void unbindEntity(ResourceEntity entity)
    {
        if(this.resourceEntities==null)
            this.resourceEntities = new ArrayList<>() ;
        this.resourceEntities.removeIf(re->re.getEntityId().equals(entity.getEntityId())&&re.getEntityType().equals(entity.getEntityType()));

    }

    /**
     * 判断是否有实体
     * @return
     */
    public  boolean hasEntities()
    {
        return  this.resourceEntities!=null&&this.resourceEntities.size()>0;
    }
}
